<?php
/*
Template Name: Lista Gemme Double Rose
*/
session_start();
?>
<?php get_header(); global $wpdb; ?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

var stile = "top=10, left=10, width=680, height=400, status=no, menubar=no, toolbar=no, scrollbars=yes";

function deleteAlert(nome,id,foto){
  var conBox = confirm("Sicuro di voler eliminare il prodotto: " + nome);
  if(conBox){ 
      location.href="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['REDIRECT_URL'];?>?&id="+ id + "&nome="+ foto + "&mode=remove";
  }else{
      return;
  }
}

function Popup(apri) {
  window.open(apri, "", stile);
}

function numeralsOnly(evt) {
  evt = (evt) ? evt : event;
  var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
  if (charCode > 31 && (charCode < 48 || charCode > 57)) {
    alert("Puoi inserire solo numeri!");
    return false;
  }
  return true;
}
function checkquota(massimo,quota) {
  var m = eval(massimo.value);
  var q = eval(quota.value);
  
  if ((q != "") && (m != "")) {
    if (q > m) {
      alert("L'importo inserito(" + q +" €) è maggiore del prezzo(" + m +" €) dell'oggetto scelto!");
      return false;
    }
  }else {
    alert("CAMPI NULLI!!!");
    return false;
  }
  return true;
}
var Timer;

function ScrollLeft(){
Timer = setInterval("document.getElementById('scrolladblock').scrollLeft -= 15", 80);
}

function ScrollRight(){
Timer = setInterval("document.getElementById('scrolladblock').scrollLeft += 15", 80);
}
// Only run everything once the page has completely loaded
$j = jQuery.noConflict();
// Only run everything once the page has completely loaded
$j(window).load(function(){

    // Set general variables
    // ====================================================================
    var totalWidth = 0;

    // Total width is calculated by looping through each gallery item and
    // adding up each width and storing that in `totalWidth`
    $j(".gallery__item").each(function(){
        totalWidth = totalWidth + $j(this).outerWidth(true);
    });

    // The maxScrollPosition is the furthest point the items should
    // ever scroll to. We always want the viewport to be full of images.
    var maxScrollPosition = totalWidth - $j(".gallery-wrap").outerWidth();

    // This is the core function that animates to the target item
    // ====================================================================
    function toGalleryItem($jtargetItem){
        // Make sure the target item exists, otherwise do nothing
        if($jtargetItem.length){

            // The new position is just to the left of the targetItem
            var newPosition = $jtargetItem.position().left;

            // If the new position isn't greater than the maximum width
            if(newPosition <= maxScrollPosition){

                // Add active class to the target item
                $jtargetItem.addClass("gallery__item--active");

                // Remove the Active class from all other items
                $jtargetItem.siblings().removeClass("gallery__item--active");

                // Animate .gallery element to the correct left position.
                $j(".gallery").animate({
                    left : - newPosition
                });
            } else {
                // Animate .gallery element to the correct left position.
                $j(".gallery").animate({
                    left : - maxScrollPosition
                });
            };
        };
    };

    // Basic HTML manipulation
    // ====================================================================
    // Set the gallery width to the totalWidth. This allows all items to
    // be on one line.
    $j(".gallery").width(totalWidth);

    // Add active class to the first gallery item
    $j(".gallery__item:first").addClass("gallery__item--active");

    // When the prev button is clicked
    // ====================================================================
    $j(".gallery__controls-prev").click(function(){
        // Set target item to the item before the active item
        var $jtargetItem = $j(".gallery__item--active").prev();
        toGalleryItem($jtargetItem);
    });

    // When the next button is clicked
    // ====================================================================
    $j(".gallery__controls-next").click(function(){
        // Set target item to the item after the active item
        var $jtargetItem = $j(".gallery__item--active").next();
        toGalleryItem($jtargetItem);
    });
});
</script>

<style type="text/css">
<!--
img {cursor: hand;}
div[title="Scroll left"]{
	background:url(<?php bloginfo('template_url');?>/images/freccia_sx_blu.png ) no-repeat;
	border-style: inset;
	border-width: 0;
	height:90px;
	width:25px;
	margin-right:-1px;
	cursor: hand;
}
div[title="Scroll right"]{
	background:url(<?php bloginfo('template_url');?>/images/freccia_dx_blu.png ) no-repeat;
	border-style: inset;
	border-width: 0;
	height:90px;
	width:25px;
	margin-left:-1px;
	cursor: hand;
}
div[title="Scroll left"]:active{
	background:url(<?php bloginfo('template_url');?>/images/freccia_sx_grigia.png );
	height:90px;
	width:25px;
}
div[title="Scroll right"]:active{
	background:url(<?php bloginfo('template_url');?>/images/freccia_dx_grigia.png );
	height:90px;
	width:27px;
	margin-right:-3px;
}
.box {
  width: auto;
  float: left;
}
a:link, a:visited {font-size: 12px; color:#333333; text-decoration:none;}
a:hover, a:active {font-size: 12px; color:#336600; text-decoration:none;}

.Stile30 {font-size: 11px; color: #333333; }
.Stile31 {font-size: 14px}
.Stile32 {font-size: 14px; color: #FFFFFF; }
.Stile33 {font-size: 14px; color: #333333; }
.Stile34 {color: #333333}
.Stile35 {
	font-size: 16px;
	font-weight: bold;
	color: #FFFFFF;
}
.Stile30c {font-size: 10px;}
.carattere { 
	font-size:16px; 
	padding:0px 
	}
.carattere {
	font-family: ADMUI3Lg;
}
.carattere {
	color: #800080;
}
.carattere strong {
	color: #808000;
}
.colore_oro {
	color: #666;
	font-family: ADMUI3Lg;
	font-size:14px; 
	padding:0px
}
.gallery-wrap {
	margin: 4px 0px 0px 0px; 
	overflow: hidden; 
	width: 715px;
	height: 63px;
	border: 1px solid rgb(114,134,214);
	background-color:#fff; 
	float:left; 
	/*background:url(<?php echo bloginfo('template_url');?>/images/sfondo_categorie.png); */
	/*background-repeat: repeat-x;*/
	padding-top:15px; }
.gallery { position: relative; left: 0; top: 0; float:left; }
.gallery__item { float: left; list-style: none; margin-right: 20px; }
.gallery__img { display: block; border: 0px solid #40331b; height: 40; width: 40px; }

.gallery__controls { margin-top: 10px; }
.gallery__controls-prev { cursor: pointer; float: left; }
.gallery__controls-next { cursor: pointer; float: left; }
.gallery img{ border:0px; margin:0 auto;}
-->
</style>

			<?php get_sidebar('top'); ?>
			<table width="100%" border="0" cellspacing="0" cellpadding="5">
  				<tr>
    				<td colspan="0" valign="top"><font color="#193E52" size="+2" face="Arial, Helvetica, sans-serif"><b><?php echo theme_get_meta_option($post->ID, 'theme_show_page_title') ? get_the_title() : ''?></b></font></td>
  				</tr>
			</table>
			
<?php
function TagliaStringa($stringa, $max_char){
	if(strlen($stringa)>$max_char){
		$stringa_tagliata=substr($stringa, 0,$max_char);
		$last_space=strrpos($stringa_tagliata," ");
		$stringa_ok=substr($stringa_tagliata, 0,$last_space);
		return ucfirst(strtolower($stringa_ok."..."));
	}else{
		return ucfirst(strtolower($stringa));
	}
}
function welcome_text(){
	echo
	"
<table width='90%' border='0' cellspacing='0' cellpadding='0'>
  <tr>
    <td><img src='". get_bloginfo('template_url') ."/images/doublerosecut4.jpg' width='700'></td>
  </tr>
  <tr>
    <td><p style='line-height: 24px' align='justify'>While constantly looking for new gemstone resources and new gemstone cuts, our company was able to develop the innovating DoubleRose Cut ,that still has a classical touch.
A significant stock of high quality gemstones was cut in order to offer the larger range of variety in color ,size and shapes.
Nowadays the DoubleRose Cut belongs to one of the most liked gem cuts which inspires world wide leading jewelers to create new and exclusive jewelry-masterpieces.
Only the original DobleRose Cut guaranties the quality, the origin of the material, the gemological control and precision of cut. </p></td>
  </tr>
</table>
";
}
$id_elimina=$_GET['id'];
if($_GET['mode'] == "remove"){
	$nome = $_GET['nome'];
	if (($_GET['nome'] == "") || (!isset($_GET['nome']))){
		$wpdb->query("DELETE FROM {$wpdb->prefix}preziosi WHERE id = '$id_elimina'");
		echo "<script>alert('Articolo eliminato')</script>";
	}else{
		$uploads_info = wp_upload_dir();
		$path = $uploads_info['basedir'].'/lista/lista/'.$nome;
		if(file_exists($path)){
			if(@unlink($path)){
				$wpdb->query("DELETE FROM {$wpdb->prefix}preziosi WHERE id = '$id_elimina'");
				echo "<script>alert('Articolo eliminato')</script>";
	      	}else{
				echo "<script>alert('Cancellazione foto NON avvenuta!')</script>";
			}
		}else{
			//echo "<script>alert('ERRORE: Probabilmente il file immagine non è più presente sul server.')</script>";
			$wpdb->query("DELETE FROM {$wpdb->prefix}preziosi WHERE id = '$id_elimina'");
		}
    }
}
if(isset($_GET['id_cat'])){
	$get_id_cat = $_GET['id_cat'];
	$all  = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}preziosi WHERE tipo='2' AND varietà='{$get_id_cat}'");
}else
	$all  = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}preziosi WHERE tipo='2'");
	
if(isset($_GET['schede']) && $_GET['schede']!=0)
	$schede = $_GET['schede'];
else
	$schede = 12;
	
if(isset($_GET['schede'])&&$_GET['schede']==0)	
	$tot_pagine = 1;
else
	$tot_pagine = ceil($all/$schede);
	
if(!isset($_GET['pag']))
	$pagina = 1;
else
	$pagina = $_GET['pag'];
$pagina = ($pagina*$schede)-$schede;
$schede_effettive = $schede-1;

if(isset($get_id_cat)){
	if(isset($_GET['schede'])&&$_GET['schede'] == 0)
		$lista_sql = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}preziosi WHERE tipo='2' AND varietà='$get_id_cat' ORDER BY id");
	else
		$lista_sql = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}preziosi WHERE tipo='2' AND varietà='$get_id_cat' ORDER BY id LIMIT {$pagina},{$schede}");
}else
	$lista_sql = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}preziosi WHERE tipo='2' ORDER BY id LIMIT {$pagina},{$schede}");

$conta = 1;
$conta_tot = 1;

$cat_gemme_dr_sql = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}subcat_preziosi WHERE categoria='2' ORDER BY rating DESC");
$num_cat_gemme_dr = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}subcat_preziosi WHERE categoria='2'");
?>
<div href="#" class="gallery__controls-prev">
	<img src="<?php bloginfo('template_url');?>/images/freccia_sx_blu.png" alt="" />
</div>
<div class="gallery-wrap">
	<div class="gallery clearfix">
<?php
$k = 1;
$ico_cat = 5;
foreach ($cat_gemme_dr_sql as $cat){
	$img = $cat->img;
	$nome_cat = strtolower($cat->nome);
	$id_cat = $cat->id;
	$categoria_attiva = $wpdb->get_var("SELECT id FROM {$wpdb->prefix}preziosi WHERE tipo='2' AND varietà='{$id_cat}'");
	if($categoria_attiva!=0){
		echo "
			<div class='gallery__item'>
				<a href='?id_cat=".$id_cat."'><img src='".network_home_url()."/wp-content/uploads/lista/lista/thumbnails/thumb_".$img."' class='gallery__img' alt='".ucfirst($nome_cat)."' width='40' height='40' /></a>
				<div><a href='?id_cat=".$id_cat."'><span style='color:#000; padding: 0 10 0 10;font-size:14px'>".ucfirst($nome_cat)."</span></a></div>
			</div>";
		$k++;
	}
}
?>
	</div>
</div>
<div href="#" class="gallery__controls-next">
	<img src="<?php bloginfo('template_url');?>/images/freccia_dx_blu.png" alt="" />
</div>
<?php
	$get_id_cat = $_GET['id_cat'];
	if(!$get_id_cat){
		welcome_text();
	}else{
	$nome_cat_attiva  = $wpdb->get_var("SELECT nome FROM {$wpdb->prefix}subcat_preziosi WHERE id='{$get_id_cat}'"); 
	echo "<br><p style='font-size:16px'><strong>".ucfirst($nome_cat_attiva)."</strong></p>";
?>
<table>
<tr>
<td>Item per page: </td>
<?php
if(isset($_GET['id_cat'])&&(!isset($_GET['schede']))){
	echo "<td ><a href='http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."&schede=12'>12</a></td>";
	echo "<td ><a href='http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."&schede=24'>24</a></td>";
	echo "<td ><a href='http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."&schede=36'>36</a></td>";
	echo "<td ><a href='http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."&schede=0'>ALL</a></td>";
}elseif(isset($_GET['id_cat'])&&isset($_GET['schede'])){
	echo "<td ><a href='http://".$_SERVER['HTTP_HOST'].$_SERVER['REDIRECT_URL']."?id_cat=".$_GET['id_cat']."&schede=12'>12</a></td>";
	echo "<td ><a href='http://".$_SERVER['HTTP_HOST'].$_SERVER['REDIRECT_URL']."?id_cat=".$_GET['id_cat']."&schede=24'>24</a></td>";
	echo "<td ><a href='http://".$_SERVER['HTTP_HOST'].$_SERVER['REDIRECT_URL']."?id_cat=".$_GET['id_cat']."&schede=36'>36</a></td>";
	echo "<td ><a href='http://".$_SERVER['HTTP_HOST'].$_SERVER['REDIRECT_URL']."?id_cat=".$_GET['id_cat']."&schede=0'>ALL</a></td>";	
}else{
	echo "<td ><a href='".$_SESSION['PHP_SELF']."?schede=12'>12</a></td>";
	echo "<td ><a href='".$_SESSION['PHP_SELF']."?schede=24'>24</a></td>";
	echo "<td ><a href='".$_SESSION['PHP_SELF']."?schede=36'>36</a></td>";
	echo "<td ><a href='".$_SESSION['PHP_SELF']."?schede=0'>ALL</a></td>";	
}
?>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" style="border:0px">
<?php
$id = array();
$varietà=array();
$indice = 0;
foreach($lista_sql as $lista){
	array_push($id,$lista->id);
	$nome_ita = $lista->nome_ita;
	$nome_eng = $lista->nome_eng;
	$code = $lista->code;
	$categoria = $lista->tipo;
	$sottocategoria = $lista->varietà;
	array_push($varietà,$wpdb->get_var("SELECT nome FROM {$wpdb->prefix}subcat_preziosi WHERE id='$sottocategoria' AND categoria='$categoria'"));
	$tipo = $wpdb->get_var("SELECT nome FROM {$wpdb->prefix}cat_preziosi WHERE id= '$categoria'");
	$certificato = $lista->certificato;
	$carati = $lista->carati;
	$taglio = $lista->taglio;
	$forma = $lista->forma;
	$altezza = $lista->altezza;
	$lunghezza = $lista->lunghezza;
	$profondità = $lista->profondità;
	$origine = $lista->origine;
	$note = $lista->note;
	$prezzo_carato = $lista->prezzo;
	$foto = $lista->foto;
	?>
<?php 
if($conta == 1 ) 
	echo "<tr>"; 
?>
		<td width="220" valign="top">
			<table width="220" border="0" cellpadding="0" cellspacing="0" ><!-- Tabella Contenitore Imagine e Dati -->
				<tr>
					<td width="220">
						<table width="220" border="0" cellpadding="0" cellspacing="0" style="border:0px"><!-- Tabella Imamgine e Dati -->
							<tr>
								<td width="220" height="170">
									<table width="220" height="170" border="0" align="center" cellspacing="0" style="border:0px"><!-- Tabella Immagini -->
										<tr>
											<td width="242px" height="220px" align="center" valign="middle" style="background-image:url(<?php echo get_bloginfo('template_url');?>/images/riquadro.png); background-repeat:no-repeat; background-position:center center">
                                            	<div align="center">
                                                <a href="<?php echo get_home_url()."/dettaglio-gioielli?id=".$id[$conta_tot-1]; ?>">
        		                                <img src="<?php echo network_home_url(); ?>/wp-content/uploads/lista/lista/grande_<?php echo $foto; ?>" width="200" height="200"/>
                		                        </a>
                                                </div>
                                        	</td>
										</tr>
									</table><!-- Chiudi Tabella Immagini -->
								</td>
							</tr>
							<!-- -->
						</table><!-- Chiudi Tabella Dati -->
					</td>
				</tr>
				<tr><td class="carattere" style="padding:0px 10px 0px 10px"><?php echo ucfirst($varietà[$conta_tot-1]);?></td></tr>
				<tr><td style="padding:0px 10px 10px 10px"><?php if($note!="")echo TagliaStringa($note,70);else echo "-" ?></td></tr>
   			</table><!-- Chiudi Tabella Immagini e Dati -->
		</td>
	    <td width="19">&nbsp;</td>
			<?php
				if( ( $conta%3 == 0 ) || ( $conta_tot == $schede ) || ( $conta_tot == $all ) ){
					echo "</tr>";
					echo "<tr>";
					for($i=0;$i<$conta;$i++){
			?>
			<td valign="top" style="padding-bottom:20px">
				<?php if(current_user_can('administrator')||current_user_can('editor')){ ?>
					<span class="art-button-wrapper" style="padding-left:7px;"> 
					<span class="l"> </span> 
					<span class="r"> </span> 
					<a class="art-button" href="<?php echo get_home_url()."/dettaglio-gioielli?id=".$id[$indice]; ?>">Detail</a> 
					</span>
					<span class="art-button-wrapper"> 
					<span class="l"> </span> 
					<span class="r"> </span> 
					<a class="art-button" href="<?php echo get_home_url()."/modifica-preziosi?id=".$id[$indice]; ?>">Modify</a> 
					</span>
					<span class="art-button-wrapper"> 
					<span class="l"> </span> 
					<span class="r"> </span> 
					<a class="art-button" href="javascript:deleteAlert('<?php echo addslashes($varietà[$indice]); ?>','<?php echo $id[$indice]; ?>','<?php echo $foto; ?>');">Delete</a> 
					</span>
					<!--input type="button" value="Dettagli" onClick="location.href='<?php echo get_home_url()."/dettaglio-gioielli?id=".$id[$indice]; ?>'" />
					<input type="button" value="Modifica" onClick="location.href='<?php echo get_home_url()."/modifica-preziosi?id=".$id[$indice]; ?>'" />
					<input type="button" value="Elimina" onClick="javascript:deleteAlert('<?php echo addslashes($varietà); ?>','<?php echo $id[$indice]; ?>','<?php echo $foto; ?>');" /-->
					<p>&nbsp;</p>					
				<?php } $indice++; ?>
	    	</td>
	    	<td>&nbsp;</td>    
    		<?php
					}
				echo "</tr>";
				}
				if( ($conta%3 == 0) || ($conta==$schede) )
					$conta = 1;
				else
					$conta = $conta + 1;
				$conta_tot = $conta_tot +1;			
			?>        
	<?php
	}
	?>
</table>
<div>
<table align="center" cellpadding="0" cellspacing="0">
<tr>
<?php if( isset($_GET['pag']) && ($_GET['pag'] > 1)){ ?>
<td><a href="<?php "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];?>&?pag=1"><img src="<?php echo  get_bloginfo('template_url'); ?>/images/freccialeft.png" alt="Prtimo" /></a></td>
<?php } ?>
<td>&nbsp;</td>
<?php
	if(($all/9)>10)
		if( (isset($_GET['pag']) && $_GET['pag']>5) && (($_GET['pag']%5)==0) ){
			$inizio_ciclo = $_GET['pag'] - 4;
			$fine_ciclo = $_GET['pag'] + 5;
		}else{
			$inizio_ciclo = 1;
			$fine_ciclo = 10;
		}
	else{
		$inizio_ciclo = 1;
		$fine_ciclo = $tot_pagine; 
	}
		for ($i=$inizio_ciclo; $i<=$fine_ciclo;$i++){
			?><td align="center" style="min-width:15px" background="<?php echo get_bloginfo('template_url');?>/images/bgtab3.png"><a href="<?php "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];?>&pag=<?php echo $i; ?>"><?php echo $i; ?></a><?php
	}
?>
<td>&nbsp;</td>
<?php if( (isset($_GET['pag']) && ($_GET['pag']<$tot_pagine)) || (!isset($_GET['pag']) && ($tot_pagine > 1)) ){ ?>
<td><a href="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] ?>&pag=<?php echo $tot_pagine; ?>"><img src="<?php get_bloginfo('template_url'); ?>/images/frecciaright.png" alt="Ultimo" /></a></td>
<?php } ?>
</tr>
</table>
</div>
				<?php }get_sidebar('bottom'); ?>

<?php get_footer(); ?>